﻿using System.Text;
using UnityEngine;

public class CRCJSON
{
    public string FilePath { get; set; }

    public ulong FileCRC { get; set; }
}

public class CRCData : MonoBehaviour
{
    static protected ulong[] CrcTable;
    //生成CRC32码表
    static public void GetCRCTable()
    {
        ulong Crc; int i, j;
        CrcTable = new ulong[256];
        for (i = 0; i < 256; i++)
        {
            Crc = (ulong)i;
            for (j = 8; j > 0; j--)
            {
                if ((Crc & 1) == 1)
                {
                    Crc = (Crc >> 1) ^ 0xEDB88320;
                }
                else
                {
                    Crc >>= 1;
                }
            }
            CrcTable[i] = Crc;
        }
    }

    //获取字符串的CRC校验值
    static public ulong GetCRCStr(string strInput)
    {
        GetCRCTable();
        //解决中文的问题
        byte[] buffer = Encoding.ASCII.GetBytes(strInput);
        ulong value = 0xffffffff;
        for (int i = 0; i < buffer.Length; i++)
        {
            value = (value >> 8) ^ CrcTable[(value & 0xFF) ^ buffer[i]];
        }
        return value ^ 0xffffffff;
    }
}